import CommonApi from 'services/commonApi';

import kboneAPI from 'kbone-api';

export default class WxWebUtil {
  static url = null;
  static config = null;

  static async initWxConfig() {
    let url = location.href;
    if (this.config !== null && url === this.url) {
      return this.config;
    }
    let config = await CommonApi.getWxConfig(url);
    if (config instanceof Error) {
      return config;
    }
    // wx.config({
    //   debug: false,
    //   appId: config.appid,
    //   timestamp: config.timestamp,
    //   nonceStr: config.noncestr,
    //   signature: config.sign,
    //   jsApiList: ['getLocation','updateAppMessageShareData','updateTimelineShareData']
    // });
    this.url = url;
    this.config = config;
    return config;
  }

  static sharePage() {
    kboneAPI.showActionSheet({
      itemList: ['分享给朋友', '分享到朋友圈'],
      success: ({
        tabIndex
      }) => {
        if (tabIndex === 0) {
          this.shareToAppMessage();
        } else if (tabIndex === 1) {
          this.shareToTimeline();
        }
      }
    });
  }

  static shareToAppMessage() {
    let config = this.initWxConfig();
    wx.ready(function () {
      wx.updateAppMessageShareData({
        title: '',
        desc: '',
        link: '',
        imgUrl: '',
        success: function () {

        }
      })
    });
  }

  static shareToTimeline() {
    let config = this.initWxConfig();
    wx.ready(function () {
      wx.updateTimelineShareData({
        title: '',
        link: '',
        imgUrl: '',
        success: function () {

        }
      })
    });
  }
}
